﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.BoMonService;

namespace eTMS.Forms.ManageList
{
    public partial class FrmMonHocImport : eTMS.Controls.BaseForm
    {
        public FrmMonHocImport()
        {
            InitializeComponent();
        }



        private void btnBrowser_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = eTMS.Commons.Constants.FILTER_EXCEL_FILE;
            open.Multiselect = false;
            if (open.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = open.FileName;
                DataTable tblSheet = Commons.ExcelReader.ReadExcellSheet(txtFileName.Text);
                List<string> ls = new List<string>();
                foreach (DataRow r in tblSheet.Rows)
                {
                    string st = r["TABLE_NAME"].ToString().Trim();
                    st = st.TrimStart('\'');
                    int index = st.IndexOf("$");
                    if (index > 0)
                    {
                        st = st.Substring(0, index);
                    }
                    st = st.TrimEnd('\'');
                    if (!ls.Contains(st))
                        ls.Add(st);
                }
                txtTenSheet.DataSource = ls;
            }
        }

        private void txtTenSheet_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtTenSheet.SelectedIndex >= 0)
            {
                DataTable tblColumns = Commons.ExcelReader.ReadExcellColumn(txtFileName.Text, txtTenSheet.Text);
                txtMaSo.DisplayMember = "COLUMN_NAME";
                txtMaSo.DataSource = tblColumns.Copy();
                txtSoHP.DisplayMember = "COLUMN_NAME";
                txtSoHP.DataSource = tblColumns.Copy();
                txtSoHPHP.DisplayMember = "COLUMN_NAME";
                txtSoHPHP.DataSource = tblColumns.Copy();
                txtHoTen.DisplayMember = "COLUMN_NAME";
                txtHoTen.DataSource = tblColumns.Copy();
                txtBoMon.DisplayMember = "COLUMN_NAME";
                txtBoMon.DataSource = tblColumns.Copy();
            }
        }
        MonHocController controller;
        private void btnRead_Click(object sender, EventArgs e)
        {
            controller.ResetTable();
            DataSet ds = Commons.ExcelReader.ReadExcellFile(txtFileName.Text, txtTenSheet.Text);
            BoMonController.LoadAllBoMon(false);
            if (ds.Tables.Count > 0)
            {
                DataTable tblTemp = ds.Tables[0];
                foreach (DataRow r in tblTemp.Rows)
                {
                    DataRow row = controller.Table.NewRow();
                    row["MonHoc_ID"] = 0;
                    if (tblTemp.Columns.Contains(txtMaSo.Text))
                    {
                        row["MaMonHoc"] = r[txtMaSo.Text];
                    }
                    if (tblTemp.Columns.Contains(txtHoTen.Text))
                    {
                        row["TenMonHoc"] = r[txtHoTen.Text];
                    }
                    if (tblTemp.Columns.Contains(txtSoHP.Text))
                    {
                        row["SoHocPhan"] = r[txtSoHP.Text];
                    }
                    if (tblTemp.Columns.Contains(txtSoHPHP.Text))
                    {
                        row["SoHocPhanHocPhi"] = r[txtSoHP.Text];
                    }
                    row["SoTietHoc"] = 0;//controller.ConvertInt(row["SoHocPhan"]) * 4;
                    row["MoTa"] = "";
                    row["BoMon_ID"] = comboBoxBoMon1.SelectedValue;
                    if (tblTemp.Columns.Contains(txtBoMon.Text))
                    {
                        string stMaBM= r[txtBoMon.Text].ToString();
                        BoMonEntity oBM=BoMonController.FindBoMonByMa(stMaBM);
                        if (oBM != null)
                        {
                            row["BoMon_ID"] = oBM.BoMon_ID;
                        }
                    }                    
                    controller.Table.Rows.Add(row);
                }
            }
            controller.AddIndexToTable();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = controller.Table;
        }

        private void FrmGiaoVienImport_Load(object sender, EventArgs e)
        {
            KhoaController.LoadAllKhoa(false);
            BoMonController.LoadAllBoMon(false);
            comboBoxKhoa1.BindData();
            controller = new MonHocController();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (controller.SaveMonHocTable())
            {
                ShowInfo("Bạn đã lưu danh sách môn học thành công!");
                controller.ResetTable();
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = controller.Table;
            }
            else
            {
                ShowWarning("Có lỗi xảy ra khi lưu dữ liệu!");
            }
        }

        private void comboBoxKhoa1_SelectedValueChanged(object sender, EventArgs e)
        {
            comboBoxBoMon1.ClearSearchValue();
            if (comboBoxKhoa1.SelectedIndex > 0)
            {
                comboBoxBoMon1.AddSearchValues("Khoa_ID", comboBoxKhoa1.SelectedValue);
            }
            comboBoxBoMon1.BindData();
        }
    }
}
